import os,time

import tools
from test_setting  import WAR_PATH,MODEL_PATH

PROGRAM_LIST,FLIE_PATH_LIST = tools.find_wars()

class Replace_settings():
    def build_catalog(self,PROGRAM_NAME): # 新建目录
        try:
            mk = r'mkdir %s\%s'%(WAR_PATH,PROGRAM_NAME)
            os.system(mk)
        except Exception as e:
            tools.logger.error('未创建成功！报错：%s'%e)
        else:
            tools.logger.info('%s执行成功，%s创建成功！'%(mk,PROGRAM_NAME))
    def copy_war(self,PROGRAM_NAME):#复制
        cp = r'copy %s\%s.war %s\%s' % (WAR_PATH,PROGRAM_NAME, WAR_PATH,PROGRAM_NAME)
        os.popen(cp)
        tools.logger.info('%s.war复制成功'%PROGRAM_NAME)
    def decompress(self,PROGRAM_NAME):  # 解压,删除压缩包
        os.chdir(r'%s\%s'%(WAR_PATH,PROGRAM_NAME))#切目录
        dba = r'jar -xvf %s.war'%PROGRAM_NAME #解压
        os.popen(dba)
        tools.logger.info('%s.war解压成功' % PROGRAM_NAME)
    def detele_war(self,PROGRAM_NAME):
        del_dba = r'del %s\%s\%s.war'% (WAR_PATH,PROGRAM_NAME,PROGRAM_NAME)  # 删除war
        os.popen(del_dba)
        tools.logger.info('本地原%s.war删除成功' % PROGRAM_NAME)
    def replace(self,PROGRAM_NAME):# 替换配置文件
        xcopy_model = r'xcopy /s /y %s\%s %s\%s' % (MODEL_PATH,PROGRAM_NAME, WAR_PATH,PROGRAM_NAME)
        try:
            os.popen(xcopy_model)
        except Exception as e:
            raise Exception("替换配置执行失败，报错：%s" % e)
        tools.logger.info('%s配置文件替换成功' % PROGRAM_NAME)
    def rename_jar(self,PROGRAM_NAME):#tomcat版本为8.5.56时，不修改接口无法使用，例如客户信息等
        if os.path.exists(r'%s\%s\WEB-INF\lib\woodstox-core-asl-4.1.2.jar'%(WAR_PATH,PROGRAM_NAME)):
            os.chdir(r'%s\%s\WEB-INF\lib'%(WAR_PATH,PROGRAM_NAME))
            rename_jar = r'ren woodstox-core-asl-4.1.2.jar awoodstox-core-asl-4.1.2.jar'
            try:
                os.popen(rename_jar)
            except Exception as e:
                raise Exception("修改jar包名执行失败，报错：%s"%e)
            tools.logger.info(r'woodstox-core-asl-4.1.2.jar改名成功！')
        else:
            tools.logger.info('无woodstox-core-asl-4.1.2.jar')
    def compress(self,PROGRAM_NAME):
        os.chdir(r'%s\%s'%(WAR_PATH,PROGRAM_NAME))
        try:
            os.popen('jar -cvf %s.war *'%PROGRAM_NAME)
        except Exception as e:
            raise Exception("打成war包执行失败，报错：%s" % e)
        tools.logger.info('%s打包成war包完成！'%PROGRAM_NAME)
        os.popen(r'del %s\%s.war'%(WAR_PATH,PROGRAM_NAME))
    @classmethod
    def del_local_files(self):
        req = os.listdir(WAR_PATH)
        if req:
            cmd = r'%s && rd /s /q %s'%(WAR_PATH[:2],WAR_PATH)
            os.popen(cmd)
            tools.logger.info('本地目录%s已清空！'%WAR_PATH)
            cmd1 = r'mkdir %s'%WAR_PATH
            os.popen(cmd1)
        else:
            tools.logger.info('本地目录%s为空！' % WAR_PATH)
    def auto_replace(self):
        if PROGRAM_LIST!='a':
            for PROGRAM_NAME in PROGRAM_LIST:
                self.build_catalog(PROGRAM_NAME)
                time.sleep(3)
                self.copy_war(PROGRAM_NAME)
                time.sleep(3)
                self.decompress(PROGRAM_NAME)
                time.sleep(15)
                self.replace(PROGRAM_NAME)
                time.sleep(3)
                self.rename_jar(PROGRAM_NAME)
                time.sleep(2)
                self.detele_war(PROGRAM_NAME)
                time.sleep(2)
                self.compress(PROGRAM_NAME)
if __name__ == '__main__':
    run = Replace_settings()
    run.auto_replace()









